/**
 * 工作组树
 * java类：com.augurit.common.system.common.GroupTreeNode 
 * @class GroupTree
 * @extends Ext.tree.TreePanel
 */
Ext.namespace("Ext.ux");

Ext.ux.GroupTree = Ext.extend(Ext.tree.TreePanel, {

	// 获取数据的URL
	url: '',
	
	// 是否使用箭头图标
	useArrows: true,
	
	// 是否显示动画
	animate: true,
	
	// 是否允许拖放
	enableDD: true,
	
	// 是否显示边框
	border: false,
	
	// 是否显示根节点
	rootVisible: true,
	
	// 是否自动显示滚动条
	autoScroll: true,
	containerScroll: true,
	
	// 设置间隔样式
	bodyStyle: 'padding:3px',

	// 组件初始化
	initComponent: function(){
	
		// 根节点
    	this.root = new Ext.tree.AsyncTreeNode({
			text: '工作组树',
			draggable: false,	// 不允许拖放
			leaf: false,		// 非叶子节点
			iconCls: 'icon-tree-root',
			partyType: 'ROOT',	//节点类型
			canManager: false,	//默认为不能管理工作组
			
			parentOrgId: null,	//工作组结点的隶属机构id
			parentOrgName: null,	//工作组结点的隶属机构名称
			parentGroupId: null,	//工作组、岗位结点的父工作组id
			parentGroupName: null,	//工作组、岗位结点的父工作组名称
			parentPosId: null,		//岗位的父岗位id
			parentPosName: null		//岗位的父岗位名称
		});
		
		// 数据加载器
		this.loader = new Ext.tree.TreeLoader({
			dataUrl: this.url,
			listeners: {
				"beforeload": function(treeLoader, node){
					treeLoader.baseParams.partyType = node.attributes.partyType;
					
					treeLoader.baseParams.orgId = null;
					treeLoader.baseParams.groupId = null;
					treeLoader.baseParams.posId = null;
					//如果是机构类型的结点
					if(node.attributes.partyType == 'ORG'){
						treeLoader.baseParams.orgId = node.attributes.orgId;
					}
					//如果是工作组类型的结点
					if(node.attributes.partyType == 'GROUP'){
						treeLoader.baseParams.groupId = node.attributes.groupId;
					}
					//如果是岗位类型的结点
					if(node.attributes.partyType == 'POSITION'){
						treeLoader.baseParams.posId = node.attributes.posId;
					}
					
					treeLoader.baseParams.text = node.text;
					treeLoader.baseParams.parentOrgId = node.attributes.parentOrgId;
					treeLoader.baseParams.parentOrgName = node.attributes.parentOrgName;
					treeLoader.baseParams.parentGroupId = node.attributes.parentGroupId;
					treeLoader.baseParams.parentGroupName = node.attributes.parentGroupName;
					treeLoader.baseParams.parentPosId = node.attributes.parentPosId;
					treeLoader.baseParams.parentPosName = node.attributes.parentPosName;
					
				}
			}
		});
		
		this.initRootCanManage();
		
		// 调用父类初始化
		Ext.ux.GroupTree.superclass.initComponent.call(this);
		
		
		//新增根节点的右键菜单
		var rootMenu = new Ext.menu.Menu({
			items: [{
				text: '新增工作组',
				iconCls: 'add-workgroup',
				handler: this.addGroup,
				scope: this
			},'-',{
				text: '刷新',
				iconCls: 'refresh',
				handler: this.refreshNode,
				scope: this
			}]
		});
		
		var orgMenu = new Ext.menu.Menu({
			id: 'orgMenu',
			items: [{
				text: '新增下级工作组',
				iconCls: 'add-workgroup',
				handler: this.orgAddGroup,
				scope: this
			},'-',{
				text: '刷新',
				iconCls: 'refresh',
				handler: this.refreshNode,
				scope: this
			}]
		});
		
		// 定义机构节点的右键菜单
		var groupMenu = new Ext.menu.Menu({
			items: [{
				text: '新增下级工作组',
				iconCls: 'add-workgroup',
				handler: this.groupAddChildGroup,
				scope: this
			},'-',{
				text: '新增下级岗位',
				iconCls: 'add-position',
				handler: this.groupAddPosition,
				scope: this
			},'-',{
				text: '新增工作组用户',
				iconCls: 'add-user',
				handler: this.groupSelectUser,
				scope: this
			},'-',{
				text: '删除本级工作组',
				iconCls: 'delete-workgroup',
				handler: this.deleteGroup,
				scope: this
			},'-',{
				text: '刷新',
				iconCls: 'refresh',
				handler: this.refreshNode,
				scope: this
			}]
		});
		
		
		// 定义岗位节点的右键菜单
		var positionMenu = new Ext.menu.Menu({
			items: [{
				text: '新增下级岗位',
				iconCls: 'add-child-position',
				handler: this.addChildPosition,
				scope: this
			},'-',{
				text: '新增岗位用户',
				iconCls: 'add-user',
				handler: this.positionAddUser,
				scope: this
			},'-',{
				text: '删除本级岗位',
				iconCls: 'delete-position',
			    handler: this.deletePostion,
			    scope: this
			},'-',{
				text: '刷新',
				iconCls: 'refresh',
				handler: this.refreshNode,
				scope: this
			}]
		});
		
		// 定义岗位用户节点的右键菜单
		var posUserMenu = new Ext.menu.Menu({
			items: [{
				text: '删除岗位用户',
				iconCls: 'delete-user',
				handler:this.deleteUserFromPos,
				scope: this
			}]
		});
		
		// 定义工作组用户节点的右键菜单
		var groupUserMenu = new Ext.menu.Menu({
			items: [{
				text: '删除工作组用户',
				iconCls: 'delete-user',
				handler:this.deleteUserFromGroup,
				scope: this
			}]
		});
		
		// 注册右键菜单及菜单实现
		this.on('contextmenu', contextMenu, this);
		function contextMenu(node, e) {
			if(node.attributes.canManager){
				e.preventDefault();
				node.select();
				
				switch(node.attributes.partyType){
					case 'ROOT':
						rootMenu.showAt(e.getXY());
						break;
					case 'ORG':
						orgMenu.showAt(e.getXY());
						break;
					case 'GROUP':
						groupMenu.showAt(e.getXY());
						break;
					case 'POSITION':
						positionMenu.showAt(e.getXY());
						break;
					case 'USER':
						if(node.parentNode.attributes.partyType.indexOf('GROUP') == 0){
							groupUserMenu.showAt(e.getXY());
						}
						else{
							posUserMenu.showAt(e.getXY());
						}
						break;
					default:
						break;
				}
			}
			else{
				Ext.Msg.alert('系统提示', '您没有访问本节点的权限');
			}
		}
		
		// 展开根节点
		this.getRootNode().expand();
	},
	
	// 组件事件初始化
	initEvents: function(){
		
    },
    //如果是系统管理员，则根节点可管理
	initRootCanManage: function(){
		Ext.Ajax.request({
			url: 'om-user!isAdminUser.action',
			success: function(response){
				if(Ext.decode(response.responseText).canManager)
					this.root.attributes.canManager = true;
			},
			scope: this
		});
	},
	//添加顶级工作组
	addGroup: function(){
		var inputWin = new Ext.ux.org.omgroup.GroupDetailWindow({id:'groupDetailWindow', title: '新增工作组'});
		inputWin.show(); 
	},
	//机构添加工作组
	orgAddGroup: function(){
		var node = this.getSelectionModel().getSelectedNode();
		
		//父机构ID和名称
		var parentOrgId = node.attributes.orgId;
		var parentOrgName = node.text;
		
		var inputWin = new Ext.ux.org.omgroup.GroupDetailWindow({
			id: 'groupDetailWindow', 
			title: '新增下级工作组'
		});
		inputWin.initParentOrg(parentOrgId, parentOrgName);
		inputWin.show();
	},
	//工作组添加下级工作组
	groupAddChildGroup: function(){
		var node = this.getSelectionModel().getSelectedNode();
		var parentGroupId = node.attributes.groupId;
		var parentGroupName = node.text;
		
		//父机构ID和名称
		var parentOrgId = node.attributes.parentOrgId;
		var parentOrgName = node.attributes.parentOrgName;
		
		var inputWin = new Ext.ux.org.omgroup.GroupDetailWindow({
			id: 'groupDetailWindow', 
			title: '新增下级工作组' 
		});
		inputWin.initParentOrg(parentOrgId, parentOrgName);
		inputWin.initParentGroup(parentGroupId, parentGroupName);
		inputWin.show();
	},
	//新增子岗位
	addChildPosition: function(){
		var node = this.getSelectionModel().getSelectedNode();
		var parentPosId = node.attributes.posId;
		var parentPosName = node.text;
		var groupId = node.attributes.parentGroupId;
		var groupName = node.attributes.parentGroupName;
		this.openPosInputWin(groupId, groupName, parentPosId, parentPosName);
	},
	//岗位新增用户
	positionAddUser: function(){
		var node = this.getSelectionModel().getSelectedNode();
		var posId = node.attributes.posId;
		var groupId = node.attributes.parentGroupId;
		var orgId = node.attributes.parentOrgId;
		this.selectUserToPos(groupId, orgId, posId, '新增岗位用户');
	},
	//工作组新增岗位
	groupAddPosition: function (){
		var node = this.getSelectionModel().getSelectedNode();
		var groupId = node.attributes.groupId;
		var groupName = node.text;
		this.openPosInputWin(groupId, groupName, null, null);
	},
	
	//新增下级岗位
	openPosInputWin: function(parentGroupId, parentGroupName, parentPosId, parentPosName) {
		var win = new Ext.ux.org.omgroup.GroupPosDetailWindow({
			title: '新增下级岗位'
		});
		win.initParentPosition(parentPosId, parentPosName);
		win.initParentGroup(parentGroupId, parentGroupName);
		win.show();
	},
	//添加已有用户到工作组
	groupSelectUser: function (){
		var node = this.getSelectionModel().getSelectedNode();
		var groupId = node.attributes.groupId;
		var orgId = node.attributes.parentOrgId;
		this.selectUserToGroup(groupId, orgId, '新增工作组用户');
	},
	//选择用户成为工作组用户
	selectUserToGroup: function(groupId, orgId, winTitle){
		var win = new Ext.ux.org.omgroup.SelectUserWindow({
			title: winTitle, 
			groupId: groupId,
			url: 'om-user!getUsersInOrgButNotInGroup.action?orgId='+ orgId + '&groupId=' + groupId
		});
		win.show();
	},
	//选择用户成为岗位用户
	selectUserToPos: function(groupId, orgId, posId, winTitle){
		var win = new Ext.ux.org.omgroup.SelectUserWindow({
			title: winTitle, 
			url: 'om-user!getUsersNotHavePos.action?orgId='+ orgId + '&posId=' + posId,
			orgId: orgId,
			groupId: groupId,
			posId: posId
		});
		win.show();
	},

	
	//节点刷新
	refreshNode: function(){
		var node = this.getSelectionModel().getSelectedNode();
		if(node != null && node.attributes.partyType != 'USER'){
			node.reload();
			
		}else{
			this.root.reload();
		}
	},

	//刷新父节点
	refreshParentNode: function(){
		var node = this.getSelectionModel().getSelectedNode();
		var parNode;
		if(node != null){
			parNode = node.parentNode;
		}
		if(parNode != null && parNode.attributes.partyType != 'USER'){
			parNode.reload();
		}else{
			this.root.reload();
		}
	},

	deleteGroup: function(){
		var node = this.getSelectionModel().getSelectedNode();
		var groupId = node.attributes.groupId;
	
		Ext.MessageBox.show({
		    title: '系统提示',
		    msg: '确认要删除选中的工作组吗？<br/><font color="red">注意</font>：删除工作组将删除该工作组下的所有子工作组、岗位和用户。',
		    buttons: Ext.Msg.YESNO,
		    icon: Ext.MessageBox.QUESTION,
		    width: 350,
		    fn: function(btn){
		    	if(btn == 'yes'){
			    	Ext.Ajax.request({
						url:'om-group!delete.action',
						success:function(){
							Ext.getCmp('groupTree').refreshParentNode();
							if(Ext.getCmp('omGroupQueryPanel') != null)
								Ext.getCmp('omGroupQueryPanel').loadData();
						},
						failure: function(form, action) {
							Ext.Msg.alert('系统提示', '删除工作组失败！');
						},
						method: 'POST',
						params:{
							groupIds: groupId
						},
						waitMsg : '正在删除数据，稍候...'
					});
				}
			},
			scope: this
		});
	},
	deletePostion: function(){
		var node = this.getSelectionModel().getSelectedNode();
		var posId = node.attributes.posId;

		Ext.MessageBox.show({
		    title: '系统提示',
		    msg: '确认要删除选中的岗位吗？<br/><font color="red">注意</font>：删除岗位将删除该岗位下的所有子岗位。',
		    buttons: Ext.Msg.YESNO,
		    icon: Ext.MessageBox.QUESTION,
		    fn: function(btn){
		    	if(btn == 'yes'){
					Ext.Ajax.request({
						url:'om-position!delete.action',
						success:function(){
							Ext.getCmp('groupTree').refreshParentNode();
						},
						failure: function(form, action) {
							Ext.Msg.alert('系统提示', '删除岗位失败！');
						},
						method: 'POST',
						params:{
							posIds: posId
						},
						waitMsg : '正在删除数据，稍候...'
						
					});
				}
			},
			width: 350,
			scope: this
		});  
	},
	deleteUser: function(){
		var node = this.getSelectionModel().getSelectedNode();
		var userId = node.attributes.userId;

		Ext.MessageBox.show({
		    title: '系统提示',
		    msg: '确认要删除选中的用户吗？',
		    buttons: Ext.Msg.YESNO,
		    icon: Ext.MessageBox.QUESTION,
		    fn: function(btn){
		    	if(btn == 'yes'){
					Ext.Ajax.request({
						url:'om-user!delete.action',
						success:function(){
							this.refreshParentNode();
						},
						failure: function(form, action) {
							Ext.Msg.alert('系统提示', '删除用户失败！');
						},
						params:{
							userIds: userId
						},
						waitMsg: '正在删除数据，稍候...',
						scope: this
					});
				}
			},
			width: 350,
		    scope: this
		});
	},
	//删除工作组用户
	deleteUserFromGroup: function(){
		var node = this.getSelectionModel().getSelectedNode();
		var userId = node.attributes.userId;
		var groupId = node.parentNode.attributes.groupId;
		
		Ext.MessageBox.show({
		    title: '系统提示',
		    msg: '确认要删除选中的用户吗？',
		    buttons: Ext.Msg.YESNO,
		    icon: Ext.MessageBox.QUESTION,
		    fn: function(btn){
		    	if(btn == 'yes'){
					Ext.Ajax.request({
						url:'om-user!deleteFromGroup.action',
						success:function(){
							this.refreshParentNode();
						},
						failure: function(form, action) {
							Ext.Msg.alert('系统提示', '删除用户失败！');
						},
						params:{
							userIds: userId,
							groupId: groupId
						},
						waitMsg: '正在删除数据，稍候...',
						scope: this
					});
				}
			},
			width: 350,
		    scope: this
		});
	},
	//删除岗位用户
	deleteUserFromPos: function(){
		var node = this.getSelectionModel().getSelectedNode();
		var userId = node.attributes.userId;
		var posId = node.parentNode.attributes.posId;
		
		Ext.MessageBox.show({
		    title: '系统提示',
		    msg: '确认要删除选中的用户吗？',
		    buttons: Ext.Msg.YESNO,
		    icon: Ext.MessageBox.QUESTION,
		    fn: function(btn){
		    	if(btn == 'yes'){
					Ext.Ajax.request({
						url:'om-user!deleteFromPos.action',
						success:function(){
							this.refreshParentNode();
						},
						failure: function(form, action) {
							Ext.Msg.alert('系统提示', '删除用户失败！');
						},
						params:{
							userIds: userId,
							posId: posId
						},
						waitMsg: '正在删除数据，稍候...',
						scope: this
					});
				}
			},
			width: 350,
		    scope: this
		});
	}
	
});